package com.suyang.syt.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.dao.interfaces.WeixinUserDao;
import com.suyang.syt.model.User;
import com.suyang.syt.model.WeixinUser;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;
import com.suyang.syt.weixin.WeixinKit;

@Repository
public class WeixinUserDaoImpl extends BaseDaoImpl<WeixinUser> implements WeixinUserDao {

	@Autowired
	private UserDao userDao;

	@Override
	public WeixinUser loadByOpenid(String openid) {
		String hql = " from WeixinUser where openid=? ";
		List<WeixinUser> list = this.list(hql, openid);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void clear() {
		String hql = " delete from WeixinUser ";
		this.executeByHql(hql);
	}

	@Override
	public void updateByOpenid(WeixinUser wuser) {
		WeixinUser weixinUser = loadByOpenid(wuser.getOpenid());
		if (EmptyUtil.isNotEmpty(weixinUser)) {
			weixinUser.setCountry(wuser.getCountry());
			weixinUser.setProvince(wuser.getProvince());
			weixinUser.setCity(wuser.getCity());
			weixinUser.setGroupid(wuser.getGroupid());
			weixinUser.setGroupname(wuser.getGroupname());
			weixinUser.setHeadimgurl(wuser.getHeadimgurl());
			weixinUser.setLanguage(wuser.getLanguage());
			weixinUser.setNickname(wuser.getNickname());
			weixinUser.setOpenid(wuser.getOpenid());
			weixinUser.setRemark(wuser.getRemark());
			weixinUser.setSex(wuser.getSex());
			weixinUser.setSubscribe(wuser.getSubscribe());
			weixinUser.setSubscribe_time(DateFormat.dateToString(new Date(Long.parseLong(wuser.getSubscribe_time()) * 1000)));
			weixinUser.setUnionid(wuser.getUnionid());
			this.update(weixinUser);
		}
	}

	@Override
	public void updateByOpenid(String openid, String remark) {
		WeixinUser weixinUser = loadByOpenid(openid);
		if (EmptyUtil.isNotEmpty(weixinUser)) {
			weixinUser.setRemark(remark);
			this.update(weixinUser);
		}
	}

	@Override
	public long countByOpenid(String openid) {
		String hql = " select count(*) from WeixinUser where openid=? ";
		return (Long) this.queryByHql(hql, openid);
	}

	@Override
	public User updateAuthorize(String openid) {
		WeixinUser weixinUser = loadByOpenid(openid);
		if (EmptyUtil.isNotEmpty(weixinUser)) {
			weixinUser.setSubscribe(1);
			weixinUser.setSubscribe_time(DateFormat.dateToString(new Date()));
			this.update(weixinUser);
		} else {
			if (this.countByOpenid(openid) <= 0) {
				List<WeixinUser> list = WeixinKit.updateBatchUser(openid);
				if (list != null && list.size() == 1) {
					WeixinUser wuser = list.get(0);
					User user = new User();
					user.setCreateName("微信");
					user.setCreateTime(DateFormat.dateToString(new Date()));
					user.setNickname(wuser.getNickname());
					user.setIcon(wuser.getHeadimgurl());
					user.setStatus(1);
					userDao.add(user);
					weixinUser = new WeixinUser(openid);
					weixinUser.setSubscribe(1);
					weixinUser.setSubscribe_time(DateFormat.dateToString(new Date()));
					weixinUser.setUid(user.getId());
					this.add(weixinUser);
					updateByOpenid(wuser);
				}
			}
		}
		return userDao.load(weixinUser.getUid());
	}

	@Override
	public void updateBatchmoveGroup(String openid_list, int groupid, String groupname) {
		StringBuilder sb = new StringBuilder(" update WeixinUser ");
		sb.append(" set groupid=").append(groupid);
		sb.append(" , groupname='").append(groupname).append("'");
		sb.append(" where openid in(").append(StringUtil.getSqlId(openid_list)).append(")");
		System.out.println(sb.toString());
		this.executeByHql(sb.toString());
	}

	@Override
	public WeixinUser loadByUid(long uid) {
		String hql = " from WeixinUser where uid=?";
		List<WeixinUser> list = this.list(hql, uid);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

}